var express = require('express');
var router = express.Router();
var tokenUtils = require('../common/jwt');
var dbaUtils = require('../common/dba');
//获取token
router.get('/getToken', function (req, res, next) {
    var token = tokenUtils.createToken({
        "iss": "lak project",
        "name": "lak",
        "aud": 'all'
    }, 3600)
    if (token) {
        res.json({result: token})
    }
});
//用户登陆
router.post('/', function (req, res, next) {
    var sql = 'SELECT * from sys_user WHERE username="' + req.body.username + '" AND password="' + req.body.password + '"';
    dbaUtils.handleDba(sql,req.headers.authorization,(result)=>{
        console.log(result)
        res.json({result: result})
    })
});
//用户登陆
router.post('/moreHandle', function (req, res, next) {
    var sql = 'SELECT * from sys_user WHERE username="' + req.body.username + '" AND password="' + req.body.password + '"';
    dbaUtils.handleDba(sql,req.headers.authorization,(result)=>{
        console.log(result)
        res.json({result: result})
    })
});

//用户注册
router.post('/register', function (req, res, next) {
    var r_sql='SELECT * FROM sys_user WHERE username="'+req.body.username+'"';
    dbaUtils.crud(r_sql,(arr)=>{
        if(arr.length>0)
        {
            res.json({result: '该账号已注册!'})
        }
        else {
            var c_sql = 'insert into sys_user (`username` ,`password`) values ("' + req.body.username + '","' + req.body.password + '")';
            dbaUtils.handleDba(c_sql,req.headers.authorization,(result)=>{
                res.json({result: '注册成功!'})
            })
        }
    })

});
//用户修改密码
router.post('/reset.password', function (req, res, next) {
    var r_sql='SELECT * FROM sys_user WHERE username="'+req.body.username+'"';
    dbaUtils.crud(r_sql,(arr)=>{
        if(arr.length>0)
        {
            var u_sql ='UPDATE sys_user SET  password="'+req.body.password+'" WHERE username="'+req.body.username+'"';
            dbaUtils.handleDba(u_sql,req.headers.authorization,(result)=>{
                res.json({result: '密码修改成功'})
            })
        }
        else {
            res.json({result: '该账号不存在'})
        }
    })
});




module.exports = router;
